//
//  KKHomeTopView.swift
//  MVVMSwfit
//
//  Created by CarShare on 2018/12/7.
//  Copyright © 2018年 CarShare. All rights reserved.
//

import UIKit


class KKHomeTopView: UIView {

    enum HomeTopViewClickType {
        case type1
        case type2
        case type3
        case type4
        case type5
        case type6
        case type7
        case type8
    }
    
    @IBOutlet weak var topBgView: UIView!
    @IBOutlet weak var topBtnView: UIView!
    @IBOutlet weak var adImgLeft: UIImageView!
    @IBOutlet weak var adImgRight: UIImageView!
    
    @IBOutlet weak var botomBtnView: UIView!
    
    @IBAction func topBtnClick(_ sender: UITapGestureRecognizer) {
        let locationPoint = sender.location(in: topBtnView);
        let btn_w = topBtnView.frame.width / 4
        let btn_H = topBtnView.frame.height
        
        let locationFrame1 = CGRect(x: btn_w * 0, y: 0, width: btn_w, height: btn_H)
        let locationFrame2 = CGRect(x: btn_w * 1, y: 0, width: btn_w, height: btn_H)
        let locationFrame3 = CGRect(x: btn_w * 2, y: 0, width: btn_w, height: btn_H)
        let locationFrame4 = CGRect(x: btn_w * 3, y: 0, width: btn_w, height: btn_H)
        
        if locationFrame1.contains(locationPoint) {
            clickAtionType(type: .type1)
        }else if locationFrame2.contains(locationPoint) {
            clickAtionType(type: .type2)
        }else if locationFrame3.contains(locationPoint) {
            clickAtionType(type: .type3)
        }else if locationFrame4.contains(locationPoint) {
            clickAtionType(type: .type4)
        }
    }
    @IBAction func bottomBtnClick(_ sender: UITapGestureRecognizer) {
        let locationPoint = sender.location(in: botomBtnView);
        let btn_w = botomBtnView.frame.width / 4
        let btn_H = botomBtnView.frame.height
        
        let locationFrame5 = CGRect(x: btn_w * 0, y: 0, width: btn_w, height: btn_H)
        let locationFrame6 = CGRect(x: btn_w * 1, y: 0, width: btn_w, height: btn_H)
        let locationFrame7 = CGRect(x: btn_w * 2, y: 0, width: btn_w, height: btn_H)
        let locationFrame8 = CGRect(x: btn_w * 3, y: 0, width: btn_w, height: btn_H)
        
        if locationFrame5.contains(locationPoint) {
            clickAtionType(type: .type5)
        }else if locationFrame6.contains(locationPoint) {
            clickAtionType(type: .type6)
        }else if locationFrame7.contains(locationPoint) {
            clickAtionType(type: .type7)
        }else if locationFrame8.contains(locationPoint) {
            clickAtionType(type: .type8)
        }
    }
    
    func clickAtionType(type:HomeTopViewClickType) -> Void {
        print(type)
    }
    
    func createCustomXIBView() -> KKHomeTopView {
        let className = type(of: self)
        let classStr = NSStringFromClass(type(of: self))
        let comArr = classStr.components(separatedBy: ".")
        let name = comArr.last
        let nib = UINib(nibName: name!, bundle: Bundle.init(for: className))
        let view : KKHomeTopView = nib.instantiate(withOwner: self, options: nil).first as! KKHomeTopView
        return view
    }
    
    var contentV : KKHomeTopView!
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        contentV = createCustomXIBView()
        contentV.frame = frame
        addSubview(contentV)

    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
//        fatalError("init(coder:) has not been implemented")
//        contentV = createCustomXIBView()
//        addSubview(contentV)
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        topBtnView.layer.cornerRadius = 5.0
        adImgLeft.layer.cornerRadius = 5.0
        adImgRight.layer.cornerRadius = 5.0
        
        topBtnView.layer.shadowOffset = CGSize.init(width: 0, height: -1)
        topBtnView.layer.shadowOpacity = 0.5
        topBgView.layer.contents = UIImage.init(named: "mine")?.cgImage
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
    }
}
